package com.seanzx.dp.serializer;

import com.seanzx.dp.common.ColumnType;
import com.seanzx.dp.serializer.impl.CharSerializer;
import com.seanzx.dp.serializer.impl.NumberSerializer;
import com.seanzx.dp.serializer.impl.UnsignedNumberSerializer;
import com.seanzx.dp.serializer.impl.VarcharSerializer;

import java.util.HashMap;
import java.util.Map;

public class SerializerFactory {
    private static final Map<ColumnType, ISerializer> SERIALIZER_MAP = new HashMap<>();
    static {
        NumberSerializer oneByteNumberSerializer = new NumberSerializer(1);
        SERIALIZER_MAP.put(ColumnType.BIT, oneByteNumberSerializer);
        SERIALIZER_MAP.put(ColumnType.BOOL, oneByteNumberSerializer);
        SERIALIZER_MAP.put(ColumnType.BOOLEAN, oneByteNumberSerializer);
        SERIALIZER_MAP.put(ColumnType.TINYINT, oneByteNumberSerializer);
        SERIALIZER_MAP.put(ColumnType.SMALLINT, new NumberSerializer(2));
        SERIALIZER_MAP.put(ColumnType.MEDIUMINT, new NumberSerializer(3));
        SERIALIZER_MAP.put(ColumnType.INT, new NumberSerializer(4));
        SERIALIZER_MAP.put(ColumnType.BIGINT, new NumberSerializer(8));
        SERIALIZER_MAP.put(ColumnType.UNSIGNED_INT, new UnsignedNumberSerializer(4));
        SERIALIZER_MAP.put(ColumnType.CHAR, new CharSerializer());
        SERIALIZER_MAP.put(ColumnType.VARCHAR, new VarcharSerializer());
    }

    public static ISerializer getSerializer(ColumnType columnType) {
        return SERIALIZER_MAP.get(columnType);
    }
}
